package View.Dorm;

import Service.DormService;
import Service.ServiceImpl.DormServiceImpl;
import View.Index.Index_admin;
import domain.Dorm;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class UpdateDorm extends JFrame {

    public UpdateDorm(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("修改宿舍信息");
        this.setSize(600,607);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("请选择宿舍号:");
        label1.setBounds(99,18,95,30);
        JLabel label2= new JLabel();
        label2.setText("结果如下:");
        label2.setBounds(214,64,106,46);
        JLabel label3= new JLabel();
        label3.setText("宿舍号:");
        label3.setBounds(79,131,65,30);
        JLabel label4= new JLabel();
        label4.setText("成员1:");
        label4.setBounds(313,130,65,30);
        JLabel label5= new JLabel();
        label5.setText("成员2:");
        label5.setBounds(80,187,65,31);
        JLabel label6= new JLabel();
        label6.setText("成员3:");
        label6.setBounds(312,188,65,30);
        JLabel label7= new JLabel();
        label7.setText("成员4:");
        label7.setBounds(79,240,65,25);
        JLabel label8= new JLabel();
        label8.setText("成员5:");
        label8.setBounds(316,242,65,30);
        JLabel label9= new JLabel();
        label9.setText("成员6:");
        label9.setBounds(81,290,65,30);
        JLabel label10= new JLabel();
        label10.setText("水费:");
        label10.setBounds(315,293,65,30);
        JLabel label11= new JLabel();
        label11.setText("电费:");
        label11.setBounds(81,340,65,30);
        JLabel label12= new JLabel();
        label12.setText("宿舍长:");
        label12.setBounds(314,338,65,30);
        JLabel label13= new JLabel();
        label13.setText("宿舍评级:");
        label13.setBounds(80,387,62,30);

        //------------------JTextField--------------------
       /* JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(214,17,120,30);*/
        DormService service = new DormServiceImpl();
        List<Dorm> dorms = service.dormList();
        Vector<String> item2= new Vector<>();
        item2.add("请选择你的宿舍");
        for (int i = 0; i < dorms.size(); i++) {
            Dorm dorm = dorms.get(i);
            item2.add(dorm.getDormnum());
        }

        JComboBox comboBox2= new JComboBox(item2);
        comboBox2.setBounds(214,17,120,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setEnabled(false);
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(153,130,120,30);
        JTextField field3= new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(388,130,120,30);
        JTextField field4= new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(155,187,120,30);
        JTextField field5= new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(391,190,120,30);
        JTextField field6= new JTextField();
        field6.setText("");
        field6.setBorder(new LineBorder(Color.lightGray));
        field6.setBounds(159,238,120,30);
        JTextField field7= new JTextField();
        field7.setText("");
        field7.setBorder(new LineBorder(Color.lightGray));
        field7.setBounds(394,244,120,30);
        JTextField field8= new JTextField();
        field8.setText("");
        field8.setBorder(new LineBorder(Color.lightGray));
        field8.setBounds(155,289,120,30);
        JTextField field9= new JTextField();
        field9.setText("");
        field9.setBorder(new LineBorder(Color.lightGray));
        field9.setBounds(392,293,120,30);
        JTextField field10= new JTextField();
        field10.setText("");
        field10.setBorder(new LineBorder(Color.lightGray));
        field10.setBounds(155,339,120,30);
        JTextField field11= new JTextField();
        field11.setText("");
        field11.setBorder(new LineBorder(Color.lightGray));
        field11.setBounds(394,340,120,30);
        JTextField field12= new JTextField();
        field12.setText("");
        field12.setBorder(new LineBorder(Color.lightGray));
        field12.setBounds(153,388,120,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("查询");
        button1.setBounds(372,19,95,27);
        JButton button2= new JButton();
        button2.setText("修改");
        button2.setBounds(139,450,95,27);
        JButton button3= new JButton();
        button3.setText("取消");
        button3.setBounds(316,452,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(label8);
        this.add(label9);
        this.add(label10);
        this.add(label11);
        this.add(label12);
        this.add(label13);
        this.add(comboBox2);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(field5);
        this.add(field6);
        this.add(field7);
        this.add(field8);
        this.add(field9);
        this.add(field10);
        this.add(field11);
        this.add(field12);
        this.add(button1);
        this.add(button2);
        this.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String dormnum = (String) comboBox2.getSelectedItem();
                System.out.println(dormnum);
                DormService service = new DormServiceImpl();
                Dorm dorm = service.selectDormByDormnum(dormnum);

                field2.setText(dorm.getDormnum());
                field3.setText(dorm.getMember1());
                field4.setText(dorm.getMember2());
                field5.setText(dorm.getMember3());
                field6.setText(dorm.getMember4());
                field7.setText(dorm.getMember5());
                field8.setText(dorm.getMember6());
                field9.setText(String.valueOf(dorm.getWater()));
                field10.setText(String.valueOf(dorm.getEle()));
                field11.setText(dorm.getHead());
                field12.setText(dorm.getLevel());
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Dorm dorm = new Dorm();
                dorm.setDormnum(field2.getText());
                dorm.setMember1(field3.getText());
                dorm.setMember2(field4.getText());
                dorm.setMember3(field5.getText());
                dorm.setMember4(field6.getText());
                dorm.setMember5(field7.getText());
                dorm.setMember6(field8.getText());
                dorm.setWater(Double.valueOf(field9.getText()));
                dorm.setEle(Double.valueOf(field10.getText()));
                dorm.setHead(field11.getText());
                dorm.setLevel(field12.getText());

                DormService service = new DormServiceImpl();
                service.updateDorm(dorm);
                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame,"修改完成");
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}
